package shuashuami.hb.com.avtivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.glid.GlidUtil;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpBMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.share.ShareCUserInfo;
import shuashuami.hb.com.view.BohuiDialog;

public class BYiwanchengInfoActivity extends AbActivity {

    private TextView tvTaskNum, tvPtype, tvSize, tvDianfu, tvYongjin,
            tvTaskStatus, tvDate, tvGoodsTitle, tvGoodsMessage, tvShouhuoren, tvPhone, tvDingdanNum;


    private ImageView imgJietu1, imgJietu2, imgJietu3, imgPType, imgShangpin;

    private LinearLayout llJilu;

    private String id;

    private GlidUtil glidUtil;

    private LayoutInflater inflater;

    private Button btnSubmit;
    private Button btnBohui;
    private LinearLayout btnLianxi;
    private String qq;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_b_weiwancheng_info);
        tvTaskNum = (TextView) findViewById(R.id.tv_chave_hand_info_num);
        tvPtype = (TextView) findViewById(R.id.tv_chave_hand_info_pingtai_type);
        tvSize = (TextView) findViewById(R.id.tv_chave_hand_info_size);
        tvDianfu = (TextView) findViewById(R.id.tv_chave_hand_info_dianfuzijin);
        tvYongjin = (TextView) findViewById(R.id.tv_chave_hand_info_yongjin);
        tvTaskStatus = (TextView) findViewById(R.id.tv_chave_hand_info_renwuzhuangtai);
        tvDate = (TextView) findViewById(R.id.tv_chave_hand_info_jieshou_shijian);
        tvGoodsTitle = (TextView) findViewById(R.id.tv_chave_hand_info_title);
        tvGoodsMessage = (TextView) findViewById(R.id.tv_chave_hand_info_tuangoujia);
        tvShouhuoren = (TextView) findViewById(R.id.tv_chave_hand_info_shouhuoren);
        tvPhone = (TextView) findViewById(R.id.tv_chave_hand_info_shoujihao);
        tvDingdanNum = (TextView) findViewById(R.id.tv_chave_hand_info_digndanbianhao);

        imgPType = (ImageView) findViewById(R.id.img_chave_hand_info_pingtai_type);
        imgShangpin = (ImageView) findViewById(R.id.img_chave_hand_goos_image);

        imgJietu1 = (ImageView) findViewById(R.id.img_chave_hand_dingdanjietu);
        imgJietu2 = (ImageView) findViewById(R.id.img_chave_hand_dingdanjietu_2);
        imgJietu3 = (ImageView) findViewById(R.id.img_chave_hand_dingdanjietu_3);

        llJilu = (LinearLayout) findViewById(R.id.ll_weiwancheng_add_jilu);

        btnSubmit = (Button) findViewById(R.id.btn_have_hand_chongxin_tijiao);
        btnBohui = (Button) findViewById(R.id.btn_have_hand_chongxin_bohui);
        btnLianxi = (LinearLayout) findViewById(R.id.btn_have_hand_chongxin_lianxi);
    }

    @Override
    protected void initData() {
        setTitleBar("任务详细");
        setLeftView();
        inflater = LayoutInflater.from(this);
        glidUtil = new GlidUtil(this);
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        id = bundle.getString("id");
        getMessage();
    }

    @Override
    protected void initListener() {
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });

        btnBohui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BohuiDialog bohuiDialog = new BohuiDialog(BYiwanchengInfoActivity.this, taskId);
                bohuiDialog.show();
            }
        });

        btnLianxi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toQQ(qq);
            }
        });
    }

    private void toQQ(String qq) {
        try {
            //第二种方式：可以跳转到添加好友，如果qq号是好友了，直接聊天
            String url = "mqqwpa://im/chat?chat_type=wpa&uin=" + qq;//uin是发送过去的qq号码
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtil.showShort(this, "请检查是否安装QQ");
        }
    }

    private String taskId;

    private void getMessage() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(this);
        HttpBMethods.getWeitongguo(listener, shareCUserInfo.getId(), id);
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(BYiwanchengInfoActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            JSONObject temp = jsonObject.getJSONObject("content");
                            JSONObject info = temp.getJSONObject("task_info");
                            tvTaskNum.setText(info.getString("task_sn"));
                            tvPtype.setText(info.getString("platform_name"));
                            tvSize.setText(info.getString("num"));
                            tvDianfu.setText(info.getString("money"));
                            tvYongjin.setText(info.getString("commission"));
                            tvTaskStatus.setText(info.getString("status_text"));
                            tvDate.setText(info.getString("recievetime"));
                            tvGoodsMessage.setText(info.getString("recievetime"));
                            tvGoodsTitle.setText(info.getString("goods_title"));
                            glidUtil.uploadRound(info.getString("platform_image"), 10, imgPType);
                            //    tvGoodsMessage.setText(info.getString("goods_title"));

                            JSONObject apply_info = temp.getJSONObject("apply_info");
                            tvShouhuoren.setText(apply_info.getString("name"));
                            tvPhone.setText(apply_info.getString("mobile"));
                            tvDingdanNum.setText(apply_info.getString("order_sn"));
                            taskId = apply_info.getString("id");

                            glidUtil.uploadRound(info.getString("goods_image"), 10, imgShangpin);

                            JSONArray jsonArray = apply_info.getJSONArray("order_images");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                String url = jsonArray.getString(i);
                                if (i == 0) {
                                    glidUtil.uploadRound(url, 10, imgJietu1);
                                } else if (i == 1) {
                                    glidUtil.uploadRound(url, 10, imgJietu2);
                                } else if (i == 2) {
                                    glidUtil.uploadRound(url, 10, imgJietu3);
                                }
                            }
                            addJilu(temp.getJSONArray("task_log"));
                            JSONObject shop = temp.getJSONObject("brush_info");
                            qq = shop.getString("qq");
                        } else {
                            ToastUtil.showShort(BYiwanchengInfoActivity.this, jsonObject.getString("message"));
                        }

                    } catch (JSONException e) {
                        ToastUtil.showShort(BYiwanchengInfoActivity.this, "数据解析错误");
                    }


                    break;
                case 2:
                    String result = msg.obj.toString();
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(BYiwanchengInfoActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            ToastUtil.showShort(BYiwanchengInfoActivity.this, jsonObject.getString("message"));
                        } else {
                            ToastUtil.showShort(BYiwanchengInfoActivity.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };

    private void addJilu(JSONArray jsonArray) {
        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                View view = inflater.inflate(R.layout.c_weiwancheng_info_jilu, null);
                TextView tvId, tvDate, tvMessage;
                tvId = (TextView) view.findViewById(R.id.tv_chave_hand_dianhuahaoma);
                tvDate = (TextView) view.findViewById(R.id.tv_chave_hand_status);
                tvMessage = (TextView) view.findViewById(R.id.tv_chave_hand_date);

                tvId.setText(jsonObject.getString("brush_id"));
                tvDate.setText(jsonObject.getString("addtime"));
                tvMessage.setText(jsonObject.getString("log"));
                llJilu.addView(view);
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }
    }

    public void submit() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(2);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(this);
        HttpBMethods.submitTask(listener,
                shareCUserInfo.getId(),
                taskId);
    }


}
